﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Models;

namespace Data_Object
{
    public class DiaDiemDao: IDiaDiemDao
    {
        public List<DiaDiem> All()
        {
            string sql = "SELECT * FROM DiaDiem";
            return Db.ReadList(sql, Make, null);
        }
      
        private static Func<IDataReader, DiaDiem> Make = reader =>
           new DiaDiem
           {
               MaDiaDiem = int.Parse(reader["MaDiaDiem"].ToString()),
               MaDichVu = int.Parse(reader["MaDichVu"].ToString()),
               TenDiaDiem = reader["TenDiaDiem"].ToString(),
               MieuTa = reader["MieuTa"].ToString(),
               DiaChi = reader["DiaChi"].ToString(),
               QuocGia = reader["QuocGia"].ToString(),
               KinhDo = double.Parse(reader["KinhDo"].ToString()),
               ViDo = double.Parse(reader["ViDo"].ToString()),
               NgayTao = DateTime.Parse(reader["NgayTao"].ToString()),
               MaTaiKhoan = int.Parse(reader["MaTaiKhoan"].ToString()),

           };


        public DiaDiem Find(int id)
        {
            string sql = "SELECT * FROM DiaDiem WHERE MaDiaDiem = @MaDiaDiem";
            object[] parms = { "@MaDiaDiem", id };
            return Db.Read(sql, Make, parms);

        }

        public void Share(DiaDiem entity, TaiKhoan tkDest)
        {
            entity.MaTaiKhoan = tkDest.MaTaiKhoan;
            Insert(entity);
        }

        public void Insert(DiaDiem entity)
        {
            string sql =
           @"INSERT INTO DiaDiem (MaDichVu,TenDiaDiem,MieuTa,DiaChi,QuocGia,KinhDo,ViDo,NgayTao,MaTaiKhoan) 			
              VALUES (@MaDichVu,@TenDiaDiem,MieuTa,@DiaChi,@QuocGia,@KinhDo,@ViDo,@NgayTao,@MaTaiKhoan)";
            entity.MaTaiKhoan = Db.Insert(sql, Take(entity));
        }

        private object[] Take(DiaDiem entity)
        {
            return new object[]  
            {
                "@MaDichVu", entity.MaDichVu.ToString(),
                "@TenDiaDiem", entity.TenDiaDiem.ToString(),
                "@MieuTa", entity.MieuTa.ToString(),
                "@DiaChi", entity.DiaChi.ToString(),
                "@QuocGia", entity.QuocGia.ToString(),
                "@KinhDo", entity.KinhDo.ToString(),
                "@ViDo", entity.ViDo.ToString(),
                "@NgayTao", entity.NgayTao.ToString(),
                "@MaTaiKhoan", entity.MaTaiKhoan.ToString(),
                "@MaDiaDiem", entity.MaDiaDiem.ToString(),
            };
        }

        public void Delete(int id)
        {
            string sql =
            @"DELETE FROM DiaDiem
               WHERE MaDiaDiem = @MaDiaDiem";
            Db.Update(sql, new object[] { "@MaDiaDiem", id });
        }

        public List<DiaDiem> FindByService(int id)
        {
            string sql = "SELECT * FROM DiaDiem WHERE MaDichVu = @MaDichVu";
            object[] parms = { "@MaDichVu", id };
            return Db.ReadList(sql, Make, parms);
        }

        public DiaDiem FinByDiaDiem(DiaDiem diaDiem)
        {
            string sql = "SELECT * FROM DiaDiem WHERE MaDiaDiem = @MaDiaDiem";
            object[] parms = { "@MaDiaDiem", diaDiem.MaDiaDiem };
            return Db.Read(sql, Make, parms);
        }

        public List<DiaDiem> FindByAccount(int id)
        {
            string sql = "SELECT * FROM DiaDiem WHERE MaTaiKhoan = @MaTaiKhoan";
            object[] parms = { "@MaTaiKhoan", id };
            return Db.ReadList(sql, Make, parms);
        }

        public void Update(DiaDiem entity)
        {
            string sql =
            @"UPDATE DiaDiem
                 SET MaDichVu = @MaDichVu,
                     TenDiaDiem = @TenDiaDiem,
                     MieuTa = @MieuTa,
                     DiaChi = @DiaChi,
                     QuocGia = @QuocGia,
                     KinhDo = @KinhDo,
                     ViDo = @ViDo,
                     NgayTao = @NgayTao,
                     MaTaiKhoan = @MaTaiKhoan
               WHERE MaDiaDiem = @MaDiaDiem";

            Db.Update(sql, Take(entity));
        }
    }
}
